package com.gxa.k12.member.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gxa.k12.common.util.R;
import com.gxa.k12.member.entity.TeacherEntity;
import com.gxa.k12.member.entity.vo.TeacherQuery;
import com.gxa.k12.member.service.TeacherService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import java.util.List;

/**
 * 讲师
 *
 * @author wenjie
 * @email wenj73487@gmail.com
 * @date 2021-07-20 11:28:11
 */
@RestController
@RequestMapping("/memberService/teacher")
@Api(description = "讲师管理")
public class TeacherController {
    @Autowired
    private TeacherService teacherService;

    /**
     * 新增讲师
     * @param teacherEntity 讲师实体类
     * @return 添加成功
     */
    @ApiOperation(value = "新增讲师")
    @PostMapping("addTeacher")
    public R save(@ApiParam(name = "teacher",value = "讲师对象",required = true)
            @RequestBody TeacherEntity teacherEntity){
        teacherService.save(teacherEntity);
        return R.ok();
    }

    /**
     * 逻辑删除讲师
     * @param id    需要删除讲师的id
     * @return  删除成功
     */
    @ApiOperation(value = "根据ID删除讲师")
    @DeleteMapping("deleteById/{id}")
    public R removeTeacherById(@ApiParam(name = "id", value = "讲师ID", required = true) @PathVariable String id){
        teacherService.removeById(id);
        return R.ok();
    }

    /**
     * 根据ID修改讲师
     * @param id    需要修改讲师的id
     * @param teacher   新的讲师实体类
     * @return  修改成功
     */
    @ApiOperation(value = "根据ID修改讲师")
    @PutMapping("updateById/{id}")
    public R updateById(
            @ApiParam(name = "id", value = "讲师ID", required = true)
            @PathVariable String id,
            @ApiParam(name = "teacher", value = "讲师对象", required = true)
            @RequestBody TeacherEntity teacher){

        teacher.setId(id);
        teacherService.updateById(teacher);
        return R.ok();
    }

    /**
     * 根据ID查询讲师
     * @param id    需要查询讲师的id
     * @return  返回查询得到的讲师
     */
    @ApiOperation(value = "根据ID查询讲师")
    @GetMapping("selectById/{id}")
    public R getById(
            @ApiParam(name = "id", value = "讲师ID", required = true)
            @PathVariable String id){

        TeacherEntity teacher = teacherService.getById(id);
        return R.ok().put("item", teacher);
    }

    /**
     * 查询所有讲师
     * @return 讲师列表
     */
    @ApiOperation(value = "所有讲师列表")
    @RequestMapping("/findAll")
    public R findAllTeacher() {
        List<TeacherEntity> list = teacherService.list();
        return R.ok().put("items",list);
    }

    /**
     * 分页讲师列表
     * @param page  当前页码
     * @param limit 每页记录数
     * @return  返回分页数据
     */
    @ApiOperation(value = "分页讲师列表")
    @GetMapping("pageTeacher/{page}/{limit}")
    public R pageListTeacher(@ApiParam(name = "page",value = "当前页码",required = true)
                             @PathVariable Long page,
                             @ApiParam(name = "limit",value = "每页记录数",required = true)
                             @PathVariable Long limit){

        //创建page对象
        Page<TeacherEntity> pageTeacher=new Page<>(page,limit);
        teacherService.page(pageTeacher,null);
        List<TeacherEntity> records = pageTeacher.getRecords();
        long total = pageTeacher.getTotal();
        return R.ok().put("total",total).put("rows",records);
    }

    /**
     * 多条件查询分页讲师列表
     * @param page  当前页码
     * @param limit 每页记录数
     * @param teacherQuery  带条件的查询对象
     * @return  返回查询成功的分页数据
     */
    @ApiOperation(value = "多条件查询分页讲师列表")
    @PostMapping("pageTeacherCondition/{page}/{limit}")
    public R pageTeacherCondition(@ApiParam(name = "page",value = "当前页码",required = true)
                                  @PathVariable Long page,
                                  @ApiParam(name = "limit",value = "每页记录数",required = true)
                                  @PathVariable Long limit,
                                  @ApiParam(name = "teacherQuery", value = "查询对象")
                                  @RequestBody(required = false) TeacherQuery teacherQuery){

        //创建page对象
        Page<TeacherEntity> pageTeacher=new Page<>(page,limit);
        //构建条件
        QueryWrapper<TeacherEntity> wrapper=new QueryWrapper<>();
        //多条件组合查询
        String name = teacherQuery.getName();
        Integer level = teacherQuery.getLevel();
        String begin = teacherQuery.getBegin();
        String end = teacherQuery.getEnd();
        if(!StringUtils.isEmpty(name)){
            wrapper.like("name",name);
        }
        if(!StringUtils.isEmpty(level)){
            wrapper.like("level",level);
        }
        if(!StringUtils.isEmpty(begin)){
            wrapper.gt("gmt_create",begin);
        }
        if(!StringUtils.isEmpty(end)){
            wrapper.le("gmt_modified",end);
        }
        //调用方法实现条件查询分页
        teacherService.page(pageTeacher,wrapper);
        Long total=pageTeacher.getTotal();
        List<TeacherEntity> records = pageTeacher.getRecords();
        return R.ok().put("total",total).put("rows",records);
    }

}
